package main

import (
	"fmt"
	"time"
)

func canConstruct(ransomNote string, magazine string) bool {
	num_ransomnote := len(ransomNote)
	num_magazine := len(magazine)

	// 把ransoNOte,magazine转换为字典

	// count := make(map[byte]int)
	// for i := 0; i < num_magazine; i++ {
	// 	count[magazine[i]]++
	// }

	// for i := 0; i < num_ransomnote; i++ {
	// 	count[ransomNote[i]]--
	// 	if count[ransomNote[i]] < 0 {
	// 		return false
	// 	}

	// }
	// return true

	// 把ransoNOte,magazine转换为字典
	if num_ransomnote > num_magazine {
		return false
	}

	count := make(map[byte]int)
	for i := 0; i < num_magazine; i++ {
		// fmt.Println(num_magazine - num_ransomnote)
		count[magazine[i]]++
		// fmt.Println(count)
		if i >= num_magazine-num_ransomnote {
			count[ransomNote[i-(num_magazine-num_ransomnote)]]--
			// fmt.Println("===========")
			// fmt.Println(count)
		}

	}

	// fmt.Println(count)

	for _, vaule := range count {
		if vaule < 0 {
			return false
		}

	}
	return true
}

func main() {
	start := time.Now()
	ransomNote, magazine := "ce", "e"
	var dec bool
	dec = canConstruct(ransomNote, magazine)
	cost := time.Since(start)
	fmt.Println(dec)
	fmt.Println(cost)

}
